package com.ht.oss.config;

import cn.hutool.core.io.IORuntimeException;
import com.ht.oss.ResData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartException;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionAdvice {

    @ExceptionHandler(IORuntimeException.class)
    public ResData<Void> handleIoRuntimeException(IORuntimeException e) {
        return ResData.fail("服务器IO错误，请稍后重试");
    }

    @ExceptionHandler(MultipartException.class)
    public ResData<Void> handleMultipartException(MultipartException e) {
        if (e.getMessage().startsWith("Maximum upload size exceeded")) {
            return ResData.fail("上传文件过大，请更换文件重试");
        }
        log.error("MultipartFile 错误", e);
        return ResData.fail("上传方式错误，请重试");
    }

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public ResData<Void> handleSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) {
        log.error("数据库错误", e);
        return ResData.fail("系统错误，请稍后重试");
    }

    @ExceptionHandler(Exception.class)
    public ResData<Void> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return ResData.fail("系统错误，请稍后重试");
    }
}